package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
    查询数据库中的数据并遍历出来
    执行DQL语句
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        try(
                Connection connection = DBUtil.getConnection();
                ){

            Statement state = connection.createStatement();

            String sql="SELECT id,name,salary,title "+
                    "FROM teacher ";

            //返回一个结果集
            ResultSet res = state.executeQuery(sql);
            //遍历结果集，并输出结果
            while (res.next()){
                int id = res.getInt("id");
                int salary =res.getInt("salary");
                String name =res.getString("name");
                String title =res.getString("title");
                System.out.println(id+","+salary+","+name+","+title);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
